﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lemonc.Tools
{
    public class EncodeHelper
    {
        public static string convertBase64(string str)
        {
            byte[] bytes = Encoding.Default.GetBytes(str);
            return Convert.ToBase64String(bytes);
        }

        public static string convertUTF8(string str)
        {
            //return Uri.EscapeDataString(str);
            
            //byte[] buffer = Encoding.Default.GetBytes(str);
            //Encoding gbk;
            //gbk = Encoding.GetEncoding("GBK");
            //byte[] gb;
            //gb = gbk.GetBytes(str);
            //return Encoding.UTF8.GetString(gb);
           // return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str));

            byte[] buffer = Encoding.UTF8.GetBytes(str);

            return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(str));
        }

        public static string convertUTF82Base64(string str)
        {          
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(buffer);
        }



        public static string convertFromUTF8(string text)
        {
            byte[] bs = Encoding.Default.GetBytes(text);

            bs = Encoding.Convert(Encoding.GetEncoding("UTF-8"), Encoding.GetEncoding("GB2312"), bs);
            return Encoding.GetEncoding("GB2312").GetString(bs);
        }


        //public static string convertUTF82Base64(string str)
        //{
        //    string info = convertUTF8(str);
        //    return convertBase64(info);
        //}

        public static string convertFromBase64(string str)
        {
            byte[] outputb = Convert.FromBase64String(str);
            string orgStr = Encoding.Default.GetString(outputb);
            return orgStr;
        }

        public static string convertFromBase64TUtf8(string str)
        {
            byte[] outputb = Convert.FromBase64String(str);
            string orgStr = Encoding.UTF8.GetString(outputb);
            return orgStr;
        }

        /// <summary>
        /// 转码函数，将GBK编码转换为Base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string convertGBKToBase64(string str)
        {
            Encoding gbk;
            gbk = Encoding.GetEncoding("GBK");
            byte[] gb;
            gb = gbk.GetBytes(str);
            return Convert.ToBase64String(gb);
        }

        public static string convertFromGBKToBase64(string str)
        {
            Encoding gbk = Encoding.GetEncoding("GBK");

            byte[] outputb = Convert.FromBase64String(str);

            string orgStr = gbk.GetString(outputb);

            return orgStr;
        }

        /// <summary>
        /// 加密
        /// 20170908 by jianfengg
        /// </summary>
        /// <param name="jmstr"></param>
        /// <returns></returns>
        public static string Encrypt(string jmstr)
        {
            string ret = "";
            DataSecurity.RSAFromPkcs8 rsa = new DataSecurity.RSAFromPkcs8();
            ret=rsa.Encrypt(jmstr);
            return ret;
        }


        /// <summary>
        /// 解密
        /// 20170908 by jianfengg
        /// </summary>
        /// <param name="jmstr"></param>
        /// <returns></returns>
        public static string decrypt(string jmstr)
        {
            string ret = "";
            DataSecurity.RSAFromPkcs8 rsa = new DataSecurity.RSAFromPkcs8();
            ret = rsa.decryptData(jmstr);
            return ret;
        }
    }
}
